<?php
$objXajax->registerFunction("frmreportgeneral_load");
$objXajax->registerFunction("frmreportgeneral_productDetail");
$objXajax->registerFunction("frmreportgeneral_init");

function frmreportgeneral_init(){
   global $lang,$objDbUpdate,$tbl_postfix;
   $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
   $strCheck = createOptionByService(1,'frmreportgeneral_chkservice','frmreportgeneral',true);
   $objResponse->addAssign('frmreportgeneral_divservicettype', 'innerHTML', $strCheck);
    $objResponse->addScript('changeWaiting(1);');                      
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
   
}

function frmreportgeneral_load($arrParams){ 
    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $reporttype = $arrParams['reporttype'];
    $strservice = $arrParams['strservice'];
    $fromdate = $arrParams['fromdate'];
    $todate = $arrParams['todate'];
    $addPath = '_'.$reporttype;
    $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
    $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
    $datasum = array();
    $arrData = frmreportgeneral_createArray($datasum,$reporttype,$strservice,$dateFrom,$dateTo);
    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_reportgeneral_grid'.$addPath.'.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('frmreportgeneral_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmreportgeneral_tbl');");  
    $objResponse->addScript('changeWaiting(1);');                      
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function frmreportgeneral_createArray(&$datasum,$reporttype,$strservice,$dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix;
    $groupby = " group by DATE_FORMAT(h.timeend,'%Y-%m-%d') order by  DATE_FORMAT(h.timeend,'%Y-%m-%d') ";
    if($reporttype==2){
     $groupby = " group by h.room_id order by h.room_id ";
    }
    elseif($reporttype==3){
      $groupby = ' group by h.user_update order by h.user_update ' ; 
    }
    $sql = frmreportgeneral_createsql($reporttype,$strservice,$dateFrom,$dateTo);
    
    $sql = " select r.name as roomname,DATE_FORMAT(h.timeend,'%Y-%m-%d') as timeend,r.id as room_id, h.generate_id,
     u.username as username,sum(h.price) as price,sum(h.totalprice) as totalprice   
     from tbl_history_book_room$tbl_postfix as h join tbl_room$tbl_postfix r ON h.room_id = r.id join tbl_user as u ON u.id = h.user_update 
     where datediff('$dateFrom',h.timeend)<=0 and datediff('$dateTo',h.timeend)>=0 $groupby ";//
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $totalsum = 0;
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $arr[$i]['productprice']= $arr[$i]['totalprice'] - $arr[$i]['price'] ;      
          $totalsum  += (int)$arr[$i]['totalprice'] ;
          $pricesum  += (int)$arr[$i]['price'] ;
     } 
         
    $datasum[0]['totalprice']= $totalsum;
    $datasum[0]['price']= $pricesum;
    $datasum[0]['productprice']= $totalsum-$pricesum;
    return  $arr  ;
}

function frmreportgeneral_createsql($reporttype,$strservice,$dateFrom,$dateTo){
    global $lang;
      $arrService = explode(';',$strservice);
      $sql = '';
      for($i=0;$i<count($arrService);$i++){
          if($i!=0){
             $sql .= ' UNION ALL '; 
          }
          $sql .=  frmreportgeneral_createDetailSql($arrService[$i],$dateFrom,$dateTo);
      }
      $result ='';
      switch($reporttype){
          case 2 : $result =" select sum(total) as total , t.date_created as name from ( $sql ) as t ". ' group by t.date_created order by t.date_created desc' ; break;
          case 3 : $result = " select sum(total) as total , t.user_created, u.username as name from ( $sql ) as t join tbl_user as u on t.user_created = u.id ".' group by t.user_created order by t.user_created '; break;
          default : $result = " select sum(total) as total , t.user_created, u.name_$lang as name from ( $sql ) as t join tbl_service_type as u on t.sertype = u.id ".' group by t.sertype order by t.sertype '; break;
        
      }
      
      return $sql;
      
}
/*
* 1: khách sạn; 2: cà phê ; 3: bi da; 4: karaoke;5 massage 
* 
* 
*/
function  frmreportgeneral_createDetailSql($service,$dateFrom,$dateTo){
     global $tbl_postfix;    
    $sql ='';
    switch($service){
        case 1: $sql="SELECT total as total,DATE_FORMAT(date_created,'%Y-%m-%d') date_created , user_created,1 as sertype FROM ht_current_book$tbl_postfix 
                        where datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0";break;
        case 2: $sql="SELECT total as total,DATE_FORMAT(date_created,'%Y-%m-%d') date_created , user_created,2 as sertype FROM cf_total$tbl_postfix 
                        where datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0";break; 
        case 3: $sql="SELECT total as total,DATE_FORMAT(date_created,'%Y-%m-%d') date_created , user_created,3 as sertype FROM bd_current_book$tbl_postfix 
                        where datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0";break; 
        case 4: $sql="SELECT total as total,DATE_FORMAT(date_created,'%Y-%m-%d') date_created , user_created,4 as sertype FROM ka_current_book$tbl_postfix 
                        where datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0";break;    
        case 5: $sql="SELECT total as total,DATE_FORMAT(date_created,'%Y-%m-%d') date_created , user_created,5 as sertype FROM ms__current_book$tbl_postfix 
                        where datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0";break;    
        default: break;
    }
    return $sql;
}

function frmreportgeneral_productDetail($generate_id){
    global $objDbUpdate,$lang,$tbl_postfix,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql = " select name as productname,num as num,price as price from tbl_history_product$tbl_postfix where generate_id = '$generate_id'  ";//
   // echo $sql; die();
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $pricesum  += (int)$arr[$i]['price'] ;
     }
     $datasum[0]['price']= $pricesum;
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_reportgeneral_productdetail_grid.html');
    $oTbs->MergeBlock('blk_data', $arr); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('frmreportgeneral_productDetail', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("$('#frmreportgeneral_productDetail').dialog('open');");
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax             
    
} 

# end region process data
?>
